package com.ekuaitu.kuaitustaff.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bumptech.glide.Glide;
import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.application.MyApplication;
import com.ekuaitu.kuaitustaff.bean.CarOrderBean;

import java.text.DecimalFormat;


public class DialogOrderDetail extends Dialog {


    private TextView tv_type, tv_car_license, tv_car_distance, tv_car_duration, tv_car_longtime;
    private ImageView iv_carimg, iv_getorder;
    private String imgStr, licenseTagStr, typeStr, distanceStr, durationStr, longtimeStr;

    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
    private Context mContext;


    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {

        this.yesOnclickListener = onYesOnclickListener;
    }

    public DialogOrderDetail(Context context) {
        super(context, R.style.MyDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_car_receiveorder);
        //按空白处不能取消动画

        setCanceledOnTouchOutside(true);
        setCancelable(true);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        iv_getorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });

    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        if (typeStr != null) {
            tv_type.setText(typeStr);
        }
        if (licenseTagStr != null) {
            tv_car_license.setText(licenseTagStr);
        }
        if (distanceStr != null) {
            tv_car_distance.setText(distanceStr);
        }
        if (durationStr != null) {
            tv_car_duration.setText(durationStr);
        }
        if (longtimeStr != null) {
            tv_car_longtime.setText(longtimeStr);
        }
        if (distanceStr != null) {
            tv_car_distance.setText(distanceStr);
        }
        if (imgStr != null) {
            Glide.with(mContext).load(MyApplication.imageUrl + imgStr).into(iv_carimg);

        }

    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_car_license = (TextView) findViewById(R.id.tv_car_license);
        tv_car_distance = (TextView) findViewById(R.id.tv_car_distance);
        tv_car_duration = (TextView) findViewById(R.id.tv_car_duration);
        tv_car_longtime = (TextView) findViewById(R.id.tv_car_longtime);
        iv_carimg = (ImageView) findViewById(R.id.iv_carimg);
        iv_getorder = (ImageView) findViewById(R.id.iv_getorder);

    }

    public void setDetail(CarOrderBean.AttachmentBean attachmentBean) {

        imgStr = attachmentBean.getCarImg();
        licenseTagStr = attachmentBean.getLicenseTag();
        if (attachmentBean.getTaskNameArray().size() != 0) {
            typeStr = attachmentBean.getTaskNameArray().get(0);
        }


        distanceStr = new DecimalFormat("#.#").format(DistanceUtil.getDistance(new LatLng(Double.parseDouble(MyApplication.curPosition[0]), Double.parseDouble(MyApplication.curPosition[1])), new LatLng(attachmentBean.getLatitude(), attachmentBean.getLongitude())) / 1000);
        ;
        durationStr = new DecimalFormat("#").format(Double.valueOf(attachmentBean.getMileage()));
        longtimeStr = new DecimalFormat("#.#").format(Double.valueOf(attachmentBean.getUnReciveTime()) / 3600);

    }


    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        void onYesClick();
    }

}
